﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Threading;

namespace WeatherGod
{
    public class Logger
    {
        public static Form1 form1;

        static private void logger_list(string content)
        {
            DateTime currentTime = DateTime.Now;
            string current_record = currentTime.ToString("d") + " " + currentTime.ToString("T") + " " + content;

            form1.listBox_log.Items.Add(current_record);
            if (form1.listBox_log.Items.Count >= 150)
            {
                form1.listBox_log.Items.Clear();
            }
            form1.listBox_log.TopIndex = form1.listBox_log.Items.Count - (int)(form1.listBox_log.Height / form1.listBox_log.ItemHeight); 
        }
        private void logger_list_ex(string content)
        {
            //Thread thLoad = new Thread(new ThreadStart(delegate()
            //{
            //    StringBuilder statusBuilder = new StringBuilder();
            //    var html = "abc";
            //    this.UIInvoke(() =>
            //    {
            //        DateTime currentTime = DateTime.Now;
            //        string current_record = currentTime.ToString("d") + " " + currentTime.ToString("T") + " " + content;

            //        form1.listBox_log.Items.Add(current_record);
            //        if (form1.listBox_log.Items.Count >= 150)
            //        {
            //            form1.listBox_log.Items.Clear();
            //        }
            //        form1.listBox_log.TopIndex = form1.listBox_log.Items.Count - (int)(form1.listBox_log.Height / form1.listBox_log.ItemHeight);

            //    });
            //}));
            //thLoad.IsBackground = true;
            //thLoad.Start();   


          
            //DateTime currentTime = DateTime.Now;
            //string current_record = currentTime.ToString("T") + " " + content;
            //listBox_log.Items.Add(current_record);
            //listBox_log.TopIndex = this.listBox_log.Items.Count - (int)(this.listBox_log.Height / this.listBox_log.ItemHeight);
        }
        private static void logger_file(string content)
        {
            DateTime currentTime = DateTime.Now;
            //string fname = currentTime.ToString("yyyy") + currentTime.ToString("MM") + currentTime.ToString("dd") + ".log";
            string fname = currentTime.ToString("yyyy") + currentTime.ToString("MM") + ".log";
            FileInfo finfo = new FileInfo(fname);
            //if (finfo.Exists && finfo.Length > 2048000)
            //{
            //    finfo.Delete();
            //}
            using (FileStream fs = finfo.OpenWrite())
            {
                string current_record = currentTime.ToString("d") + " " + currentTime.ToString("T") + " " + content;
                StreamWriter w = new StreamWriter(fs);
                w.BaseStream.Seek(0, SeekOrigin.End);//设置写数据流的起始位置为文件流的末尾  
                w.Write(current_record + "\r\n");  //写入日志内容并换行
                w.Flush();
                w.Close();  //关闭写数据流  
            }
        }
        static private void logger_screen(string content)
        {
            ConsoleColor TextChange = new ConsoleColor();
            DateTime currentTime = DateTime.Now;

            TextChange.TextColor((int)ConsoleColor.Foreground.Cyan);
            Console.Write(currentTime.ToString("d") + " " + currentTime.ToString("T"));
            TextChange.ResetColor();
            if (Regex.IsMatch(content, "没有|错误|Error|异常|Exception", RegexOptions.IgnoreCase))
            {
                TextChange.TextColor((int)ConsoleColor.Foreground.Red + (int)ConsoleColor.Foreground.Intensity);
            }
            if (Regex.IsMatch(content, "接口", RegexOptions.IgnoreCase))
            {
                TextChange.TextColor((int)ConsoleColor.Foreground.Yellow + (int)ConsoleColor.Foreground.Intensity);
            }
            Console.WriteLine(" " + content);
            TextChange.ResetColor();
        }

        public static void logger(string content)
        {
            logger_file(content);
            logger_screen(content);
            logger_list(content);
        }

        public void PrintConfig()
        {
            logger("========================================================");
        }
    }
}
